
import router from '../router'
import { ShowError, ShowLoading } from '@/utils/message'
import axios from 'axios'
import { Toast } from 'vant'

export const baseURL = "https://lhx123.love:3000/"   

const request = axios.create({  
    baseURL: baseURL, 
    headers: {
    },  
    timeout: 60000, 
})

// 请求拦截器
request.interceptors.request.use(function (config) {
    let token = sessionStorage.getItem('token')
    console.log(config);
    if (!config.url.includes('lhx123.love:3300')) {
        config.headers.token = token;
    }
    ShowLoading()
    return config;
}, function (error) {
    ShowError('请求失败')
    // 请求发送失败
    return Promise.reject(error)
})

// 响应拦截器
request.interceptors.response.use(function (response) {
    Toast.clear(true);
    console.log(response.data)
    if (response.data.code == 3001) {
        ShowError(response.data.msg)
        router.push('/login')
    }
    return response.data  // axios 通过 data 接受
}, function (error) {
    ShowError('响应失败')
    return Promise.reject(error)
})

export default request;
